package com.tencreat.service.mall.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.base.Function;
import com.google.common.collect.Lists;
import com.tencreat.common.constant.TencreatIdAndName;
import com.tencreat.common.constant.TencreatMapData;
import com.tencreat.common.core.HttpResult;
import com.tencreat.common.core.domain.model.ReqIdsParam;
import com.tencreat.common.enums.mall.JumpTypeEnum;
import com.tencreat.common.exception.CustomException;
import com.tencreat.domain.mall.MallLunbo;
import com.tencreat.domain.mall.MallStorage;
import com.tencreat.domain.mall.vo.AdminLunboVO;
import com.tencreat.domain.mall.vo.ResJumpRecordVO;
import com.tencreat.domain.mall.dto.AdminLunboAddEditDTO;
import com.tencreat.domain.mall.vo.MallLunboVO;
import com.tencreat.mapper.mall.MallLunboMapper;
import com.tencreat.service.account.IMallSystemService;
import com.tencreat.service.mall.IMallLunboService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author tencreat
 * @date 2021-09-15
 */
@Service
public class MallLunboServiceImpl extends ServiceImpl<MallLunboMapper, MallLunbo> implements IMallLunboService {

    @Autowired
    private IMallSystemService mallSystemService;

    @Autowired
    private MallStorageServiceImpl mallStorageService;

    @Override
    public HttpResult<AdminLunboVO> lunboInfoData(Integer objId) {
        AdminLunboVO result = new AdminLunboVO();
        MallLunbo mallLunbo = lambdaQuery().eq(MallLunbo::getId, objId).one();
        BeanUtils.copyProperties(mallLunbo, result);

        ResJumpRecordVO jumpRecord = JSONObject.parseObject(mallLunbo.getJumpRecord(), ResJumpRecordVO.class);
        // 详情页
        if (0 < jumpRecord.getId()) {
            result.setLabel(jumpRecord.getId().toString());
        }
        // 网页
        if (StringUtils.isNoneBlank(jumpRecord.getWebViewUrl())) {
            result.setLabel(jumpRecord.getWebViewUrl());
        }
        // 视频
        if (StringUtils.isNoneBlank(jumpRecord.getVideoUrl())) {
            result.setLabel(jumpRecord.getVideoUrl());
        }

        return HttpResult.ok(result);
    }

    @Override
    public HttpResult<List<TencreatIdAndName>> locatTypeData() {

        List<Integer> parseLocatType = JSONArray
                .parseArray(mallSystemService.selectMallSystemByName("轮播图展示位置数据").getKeyValue(), Integer.class);

        return HttpResult.ok(Lists.transform(parseLocatType, new Function<Integer, TencreatIdAndName>() {

            @Override
            public TencreatIdAndName apply(Integer input) {
                return new TencreatIdAndName(input, TencreatMapData.lunboLocatType.get(input));
            }

        }));
    }

    @Override
    public HttpResult<Boolean> del(Integer id) {
        return HttpResult.ok(baseMapper.deleteById(id)>0);
    }

    @Override
    public HttpResult<String> addEdit(AdminLunboAddEditDTO adminLunboAddEditVo) {
        // 修改
        if (null != adminLunboAddEditVo.getObjId()) {
            MallLunbo mallLunbo = lambdaQuery().eq(MallLunbo::getId, adminLunboAddEditVo.getObjId()).one();
            BeanUtils.copyProperties(adminLunboAddEditVo, mallLunbo);
            mallLunbo.setJumpRecord(JSONObject.toJSONString(
                    createJumpRecordDB(adminLunboAddEditVo.getJumpType(), adminLunboAddEditVo.getLabel())));
            mallLunbo.setUpdateTime(LocalDateTime.now());
            baseMapper.updateById(mallLunbo);
            return HttpResult.ok();
        }else {
            Integer count = lambdaQuery().count();
            if (count > 5){
                throw new CustomException("最多新增5张图片");
            }
            // 新增
            MallLunbo newMallLunbo = new MallLunbo();
            BeanUtils.copyProperties(adminLunboAddEditVo, newMallLunbo);
            newMallLunbo.setCreateTime(LocalDateTime.now());
            newMallLunbo.setUpdateTime(LocalDateTime.now());
            newMallLunbo.setAreaId(-1L);
            newMallLunbo.setLocatType(adminLunboAddEditVo.getLocatType());
            newMallLunbo.setJumpRecord(JSONObject
                    .toJSONString(createJumpRecordDB(adminLunboAddEditVo.getJumpType(), adminLunboAddEditVo.getLabel())));
            save(newMallLunbo);
        }

        return HttpResult.ok();
    }

    @Override
    public HttpResult<List<MallLunboVO>> getList(Integer type) {
        List<MallLunbo> lunboList = lambdaQuery()
                .eq(MallLunbo::getLocatType,type)
                .orderByAsc(MallLunbo::getSortNum).list();
        List<MallLunboVO> mallLunboVOList=new ArrayList<>();
        for (MallLunbo mallLunbo : lunboList) {
            MallLunboVO mallLunboVO = new MallLunboVO();
            mallLunboVO.setId(mallLunbo.getId());
            mallLunboVO.setSortNum(mallLunbo.getSortNum());
            mallLunboVO.setUrl(mallLunbo.getImgUrl());
            mallLunboVO.setLocatType(mallLunbo.getLocatType());
            JSONObject jsonObject = JSONObject.parseObject(mallLunbo.getJumpRecord());
            mallLunboVO.setWebViewUrl(jsonObject.getString("webViewUrl"));
            mallLunboVO.setJumpRecord(jsonObject.getString("id"));
            mallLunboVOList.add(mallLunboVO);
        }
        return HttpResult.ok(mallLunboVOList);
    }

    @Override
    public HttpResult<MallStorage> sort(ReqIdsParam ids) {
        List<Long> list = ids.getId();
        for(int i=0;i<list.size();i++){
            Long id = list.get(i);
            MallLunbo mallLunbo = baseMapper.selectById(id);
            mallLunbo.setSortNum(i);
            baseMapper.updateById(mallLunbo);
        }
        return HttpResult.ok();
    }

    /**
     * 创建跳转对象
     *
     * @author linjinzhan
     * @date 2018年11月14日
     * @param jumpType
     * @param label
     * @return
     */
    public ResJumpRecordVO createJumpRecordDB(Integer jumpType, String label) {
        ResJumpRecordVO newJumpRecord = new ResJumpRecordVO(0, "", "");
        // 跳转类型
        if (JumpTypeEnum.WEB_VIEW.getType().equals(jumpType)) {
            // 网页
            newJumpRecord.setWebViewUrl(label);
        } else if (JumpTypeEnum.VIDEO.getType().equals(jumpType)) {
            // 视频
            newJumpRecord.setVideoUrl(label);
        } else if (JumpTypeEnum.NO_OPERATION.getType().equals(jumpType)) {
            // 无操作
        } else {
            // 其余都是详情页
            newJumpRecord.setId(Integer.parseInt(label));
        }
        return newJumpRecord;
    }
}



